#设置各种参数
# typeset ORA_PRI_IP='10.27.5.199'               #主服务器ip
# typeset ORA_SLA_IP='10.27.5.139'               #从服务器ip
# typeset ORA_PRI_TNS_PORT='@@ORA_PRI_TNS_PORT@@'
# typeset ORA_SLA_TNS_PORT='@@ORA_SLA_TNS_PORT@@'
# typeset ORA_PORT='@@ORA_PORT@@'
# typeset hmuser='oracle'                   #主服务器远程用户
# typeset hmpassword='oracle'           #主服务器远程密码
# typeset hsuser='oracle'                   #从服务器远程用户
# typeset hspassword='oracle'           #从服务器远程密码
# typeset check_times=''                  #命令重复次数
# typeset ORA_SID='orcl'                       #设置主库SID
# typeset ORA_SID_UP=$(echo "${ORA_SID}" | tr '[a-z]' '[A-Z]') #ORCL
# typeset ORA_SID_DOWN=$(echo "${ORA_SID}" | tr '[A-Z]' '[a-z]')
# typeset ssid='orcl'                       #设置从库SID
# typeset ORA_PRI_DB_UNIQUE_NAME='@@ORA_PRI_DB_UNIQUE_NAME@@' #orcl_pri
# typeset ORA_SLA_DB_UNIQUE_NAME='@@ORA_SLA_DB_UNIQUE_NAME@@' #orcl_stl
# typeset ORA_PRI_DB_UNIQUE_NAME='orcl_pri'            #设置主库uniqname
# typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl'            #设置从库uniqname
# typeset ORA_PRI_SERVICE_NAME='@@ORA_PRI_SERVICE_NAME@@' #同orcl_pri
# typeset ORA_SLA_SERVICE_NAME='@@ORA_SLA_SERVICE_NAME@@' #同orcl_stl
# typeset ORA_PRI_SERVICE_NAME='orcl_pri'
# typeset ORA_SLA_SERVICE_NAME='orcl_stl'
# typeset ORA_PRI_DB_UNIQUE_NAME_UP=$(echo "${ORA_PRI_DB_UNIQUE_NAME}" | tr '[a-z]' '[A-Z]') #ORCL_PRI
# typeset ORA_SLA_DB_UNIQUE_NAME_UP=$(echo "${ORA_SLA_DB_UNIQUE_NAME}" | tr '[a-z]' '[A-Z]') #ORCL_STL
# typeset ORA_PRI_SERVICE_NAME_UP="${ORA_PRI_DB_UNIQUE_NAME_UP}" #同ORCL_PRI
# typeset ORA_SLA_SERVICE_NAME_UP="${ORA_SLA_DB_UNIQUE_NAME_UP}" #同ORCL_STL
# typeset ORA_USER='@@ORA_USER@@' #sys
# typeset ORA_PASSWD='@@ORA_PASSWD@@' #sys
# typeset ORA_ROLE='@@ORA_ROLE@@' # 分开写，则为常量primary或者standby
# typeset ORA_BAK_HOME='@@ORA_BAK_HOME@@' 


#配置日志路径
if [ -z "$ORA_SLA_IP" ];then
    ORA_PRI_IP=$(/usr/sbin/ip a|grep `/usr/sbin/ip r|grep default|awk '{print $5}'`|grep inet|awk '{print $2}'|awk -F'/' '{print $1}')
fi
typeset SCRIPT_NAME="oracle_slave_start_init_part2_$ORA_SLA_IP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

if [ ! -f "${LOG_FILE}" ];then
    touch ${LOG_FILE}
    if [ $? -ne 0 ];then
        log_format "ERROR" "failed to create log file" && log_format "ERROR" "script_execute_error!" && exit 1
    else
        log_format "INFO" "success to create log file"
    fi
fi

log_format "begin to build oracle slave................................."

#设置oracle环境变量
source ~/.bash_profile

#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer' && log_format "ERROR" "script_execute_error!" && exit 1
fi

#在备库应用日志同步数据
sqlplus / as sysdba <<eof  | tee -a ${LOG_FILE}
select database_role,open_mode from v\$database;
eof

> /tmp/disconnet1.log
sqlplus / as sysdba <<eof > /tmp/disconnet1.log
alter database recover managed standby database using current logfile disconnect from session;
eof

cat /tmp/disconnet1.log | tee -a ${LOG_FILE}
dis1num=`cat /tmp/disconnet1.log |grep 'Database altered'|grep -v grep |wc -l`
if [ $dis1num -eq 1 ];then
    log_format "INFO" "success to alter database using current logfile disconnect from session"
else
    log_format "ERROR" "fail to alter database using current logfile disconnect from session" && log_format "ERROR" "script_execute_error!" && exit 1
fi

#启库
> /tmp/disconnet2.log
sqlplus / as sysdba <<eof  > /tmp/disconnet2.log
alter database recover managed standby database cancel;
eof

cat /tmp/disconnet2.log | tee -a ${LOG_FILE}
dis2num=`cat /tmp/disconnet2.log |grep 'Database altered'|grep -v grep |wc -l`
if [ $dis2num -eq 1 ];then
    log_format "INFO" "success to alter database recover managed standby database cancel"
else
    log_format "ERROR" "fail to alter database recover managed standby database cancel" && log_format "ERROR" "script_execute_error!" && exit 1
fi

> /tmp/disconnet3.log
sqlplus / as sysdba <<eof  > /tmp/disconnet3.log
alter database open;
alter database recover managed standby database using current logfile disconnect from session;
eof

cat /tmp/disconnet3.log | tee -a ${LOG_FILE}
dis3num=`cat /tmp/disconnet3.log |grep 'Database altered'|grep -v grep |wc -l`
if [ $dis3num -eq 2 ];then
    log_format "INFO" "success to finally alter database using current logfile disconnect from session"
else
    log_format "ERROR" "fail to finally alter database using current logfile disconnect from session" && log_format "ERROR" "script_execute_error!" && exit 1
fi
log_format "INFO" "script_execute_success!"